package com.youtellv1.controller;

import com.youtellv1.config.ResultData;
import com.youtellv1.dto.YSalDTO;
import com.youtellv1.service.YSalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/manager/ysal")
public class YSalController {
    @Autowired
    private YSalService ySalService;

    @GetMapping("/list")
    public ResultData list(@RequestParam Integer tid,
                           @RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "8") int size) {

        int total = ySalService.countById(tid);
        List<YSalDTO> list = ySalService.findByTidWithPage(tid, page, size);

        Map<String, Object> result = new HashMap<>();
        result.put("items", list);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (int) Math.ceil((double) total / size));

        return new ResultData<>(result);
    }

    @PostMapping("/add")
    @ResponseBody
    public ResultData addTeacherSal(
            @RequestParam("base_sal") Float base_sal,
            @RequestParam("subsidy") Float subsidy,
            @RequestParam("class_size") Float class_size,
            @RequestParam("health") Float health,
            @RequestParam("year") Float year,
            @RequestParam("edu") Float edu,
            @RequestParam("merits_sal1") Float merits_sal1,
            @RequestParam("merits_sal2") Float merits_sal2,
            @RequestParam("merits_sal3") Float merits_sal3,
            @RequestParam("merits_sal4") Float merits_sal4,
            @RequestParam("merits_sal5") Float merits_sal5,
            @RequestParam("full_time") Integer full_time,
            @RequestParam("safe") Float safe,
            @RequestParam("class_hour") Float class_hour,
            @RequestParam("newstu") Float newstu,
            @RequestParam("for_leave") Float for_leave,
            @RequestParam("other") Float other,
            @RequestParam("class_out") Float class_out,
            @RequestParam("add_class") Float add_class,
            @RequestParam("class_num") Integer class_num,
            @RequestParam("class_reg") Integer class_reg,
            @RequestParam("sign_num") Integer sign_num,
            @RequestParam("month") String month,
            @RequestParam("tid") Integer tid
    ) {
        YSalDTO dto = new YSalDTO();
        dto.setBase_sal(base_sal);
        dto.setSubsidy(subsidy);
        dto.setClass_size(class_size);
        dto.setHealth(health);
        dto.setYear(year);
        dto.setEdu(edu);
        dto.setMerits_sal1(merits_sal1);
        dto.setMerits_sal2(merits_sal2);
        dto.setMerits_sal3(merits_sal3);
        dto.setMerits_sal4(merits_sal4);
        dto.setMerits_sal5(merits_sal5);
        dto.setFull_time(full_time);
        dto.setSafe(safe);
        dto.setClass_hour(class_hour);
        dto.setNewstu(newstu);
        dto.setFor_leave(for_leave);
        dto.setOther(other);
        dto.setClass_out(class_out);
        dto.setAdd_class(add_class);
        dto.setClass_num(class_num);
        dto.setClass_reg(class_reg);
        dto.setSign_num(sign_num);
        dto.setMonth(month);
        dto.setTid(tid);

        ySalService.insert(dto);
        return new ResultData<>();
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultData updateTeacherSal(
            @RequestParam("sid") Integer sid,
            @RequestParam("base_sal") Float base_sal,
            @RequestParam("subsidy") Float subsidy,
            @RequestParam("class_size") Float class_size,
            @RequestParam("health") Float health,
            @RequestParam("year") Float year,
            @RequestParam("edu") Float edu,
            @RequestParam("merits_sal1") Float merits_sal1,
            @RequestParam("merits_sal2") Float merits_sal2,
            @RequestParam("merits_sal3") Float merits_sal3,
            @RequestParam("merits_sal4") Float merits_sal4,
            @RequestParam("merits_sal5") Float merits_sal5,
            @RequestParam("full_time") Integer full_time,
            @RequestParam("safe") Float safe,
            @RequestParam("class_hour") Float class_hour,
            @RequestParam("newstu") Float newstu,
            @RequestParam("for_leave") Float for_leave,
            @RequestParam("other") Float other,
            @RequestParam("class_out") Float class_out,
            @RequestParam("add_class") Float add_class,
            @RequestParam("class_num") Integer class_num,
            @RequestParam("class_reg") Integer class_reg,
            @RequestParam("sign_num") Integer sign_num,
            @RequestParam("month") String month,
            @RequestParam("tid") Integer tid
    ) {
        YSalDTO dto = new YSalDTO();
        dto.setSid(sid);
        dto.setBase_sal(base_sal);
        dto.setSubsidy(subsidy);
        dto.setClass_size(class_size);
        dto.setHealth(health);
        dto.setYear(year);
        dto.setEdu(edu);
        dto.setMerits_sal1(merits_sal1);
        dto.setMerits_sal2(merits_sal2);
        dto.setMerits_sal3(merits_sal3);
        dto.setMerits_sal4(merits_sal4);
        dto.setMerits_sal5(merits_sal5);
        dto.setFull_time(full_time);
        dto.setSafe(safe);
        dto.setClass_hour(class_hour);
        dto.setNewstu(newstu);
        dto.setFor_leave(for_leave);
        dto.setOther(other);
        dto.setClass_out(class_out);
        dto.setAdd_class(add_class);
        dto.setClass_num(class_num);
        dto.setClass_reg(class_reg);
        dto.setSign_num(sign_num);
        dto.setMonth(month);
        dto.setTid(tid);

        ySalService.update(dto);
        return new ResultData<>();
    }

    @GetMapping("/delete/{sid}")
    public ResultData<Void> deleteSalaryByGet(@PathVariable Integer sid) {
        try {
            ySalService.delete(sid);
            return new ResultData<>(200, "删除成功", null);
        } catch (Exception e) {
            return new ResultData<>(500, "删除失败：" + e.getMessage(), null);
        }
    }
}
